package demo.lock;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author Arnold Yand
 * @since Mar 10, 20173:21:16 PM
 * @summary demo.lock.CountDownLatchDemo.java
 */
public class CountDownLatchDemo {

    public static void main(String[] args) {
        int threadCount = 10;
        ExecutorService executor = Executors.newFixedThreadPool(threadCount);

        final CountDownLatch cdl = new CountDownLatch(threadCount);
        
        for (int i = 0; i < threadCount; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println("等等我。。。马上到。。。" + Thread.currentThread().getName());
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                    }
                    cdl.countDown();
                }
            });
        }

        try {
            executor.shutdown();
            executor.awaitTermination(60, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
        }
        
        System.out.println("到齐了，出发。。。");
    }

}
